﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GUI.AirportSVC;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.ServiceModel;

namespace GUI.ServiceAgents
{
    public interface IAirportServiceAgent
    {
        void ConnectToAirportService(EventHandler<newFlightUpdateReceivedEventArgs> callback);
        void RegisterForPlaneUpdates(EventHandler<UpdatePlaneReceivedEventArgs> callback);
        
        //void GetAirportLayout(EventHandler<GetAirportLayoutCompletedEventArgs> callback);
        void GetAirportLayout(EventHandler<IncomingAirportLayoutReceivedEventArgs> callback);
        void GetCurrentPlaneList(EventHandler<SendCurrentPlaneListReceivedEventArgs> callback);
        void GetSuggestion(Plane updatedPlane, Node destinationNode, EventHandler<SendSuggestionReceivedEventArgs> callback);
        void AcceptSuggestion(Suggestion acceptedSuggestion);
    }

    public class AirportServiceAgent : IAirportServiceAgent
    {
        AirportSVC.AirportServiceClient _airportSvcClient =
                    new AirportSVC.AirportServiceClient("NetTcpBinding_IAirportService");
        //AirportSVC.IAirportService _airportSvcClient;

        /*
        public AirportServiceAgent()
            : this(new AirportSVC.AirportServiceClient("NetTcpBinding_IAirportService"))
        {
        }

        public AirportServiceAgent(AirportSVC.AirportServiceClient airportService)
        {
            _airportSvcClient = airportService;
        }
        */

        // Establishes connection to AirportService and receives new flights
        public void ConnectToAirportService(EventHandler<newFlightUpdateReceivedEventArgs> callback)
        {
            _airportSvcClient.newFlightUpdateReceived += callback; 
            
            // Added to debug network problems
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                _airportSvcClient.RegisterForFlightUpdatesAsync();
            }
            else
            {
                Debug.WriteLine("AirportServiceAgent: Network Unavailable");
            }
        }

        public void RegisterForPlaneUpdates(EventHandler<UpdatePlaneReceivedEventArgs> callback)
        {
            _airportSvcClient.UpdatePlaneReceived += callback;
            _airportSvcClient.RegisterForPlaneUpdatesAsync();
        }

        public void GetAirportLayout(EventHandler<IncomingAirportLayoutReceivedEventArgs> callback)
        {
            _airportSvcClient.IncomingAirportLayoutReceived += callback;
            _airportSvcClient.GetAirportLayoutAsync();
        }

        public void GetCurrentPlaneList(EventHandler<SendCurrentPlaneListReceivedEventArgs> callback)
        {
            _airportSvcClient.SendCurrentPlaneListReceived += callback;
            _airportSvcClient.GetCurrentPlaneListAsync();
        }

        public void GetSuggestion(Plane updatedPlane, Node destinationNode, EventHandler<SendSuggestionReceivedEventArgs> callback)
        {
            try
            {
                _airportSvcClient.SendSuggestionReceived += callback;
                _airportSvcClient.GetSuggestionAsync(updatedPlane, destinationNode);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("AirportServiceAgent: {0}", ex.Message);
            }
        }

        public void AcceptSuggestion(Suggestion acceptedSuggestion)
        {
            try
            {
                _airportSvcClient.AcceptSuggestionAsync(acceptedSuggestion);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("AirportServiceAgent: {0}", ex.Message);
            }
        }
    }
}
